<?php
/**
 * Created by PhpStorm.
 * FileName: Appointment.class.php
 * User: Administrator
 * Date: 2018/4/16
 * Time: 10:02
 */

namespace Api\Controller;

use Admin\Model\UserSignModel;
use Admin\Model\VouchersModel;

class UserSignController extends ApiController {

    private $userSignModel;
    private $vouchersModel;

    public function __construct() {
        parent::__construct();
        $this->userSignModel = new UserSignModel();
        $this->vouchersModel = new VouchersModel();
    }

    // 签到列表
    public function getList() {
        $where = array();
        $user_id = I('user_id');
        if (!$user_id) {
            $this->callBack('无效的参数',$this->ERROR_CODE);
        }
        $where['user_id'] = $user_id;
        $pageSize = 15;
        $curPage = I('p',1);
        $data = $this->userSignModel->getList($where,$pageSize,$curPage);
        return $this->callBack("签到列表",$this->SUCCESS_CODE,$data);
    }

    // 添加签到
    public function add() {
        $user_id = I('user_id');
        if (!$user_id) {
            $this->callBack('无效的参数',$this->ERROR_CODE);
        }
        $currentDate = date('Y-m-d');
        $data = array(
            'user_id' => $user_id,
            'add_time' => array('between',sprintf('%s,%s',strtotime(sprintf('%s 00:00:00',$currentDate)),strtotime(sprintf('%s 23:59:59',$currentDate))))
        );
        $isSigned = $this->userSignModel->where($data)->count();
        if ($isSigned > 0) {
            return $this->callBack("今天您已签到过，明天再来吧！",$this->ERROR_CODE);
        }
        unset($data['add_time']);
        $rs = $this->userSignModel->add($data);
        if ($rs !== false) {
            // 签到后续处理
            $this->afterSign($user_id);
            return $this->callBack("签到成功！",$this->SUCCESS_CODE);
        }
        return $this->callBack("签到失败！",$this->ERROR_CODE);
    }

    private function afterSign($userId) {
        // 连续签到7天，免费获取一张通话券
        $signDays = $this->getSignDays($userId);
        if ($signDays >= 7) {
            $data = array(
                'money' => 1,
                'add_time' => time(),
                'period_date' => strtotime('+7 day'),
                'user_id' => $userId,
                'remark' => '连续签到7天，免费获赠一张通话券'
            );
            $this->vouchersModel->add($data);
        }
    }

    private function getSignDays($userId) {
        $sql = "select * from ( ";
        $sql .= "select @row_no:=@row_no+1 as row_no,@group_row:=if(@sign_date=DATE_FORMAT(date_add(FROM_UNIXTIME(add_time), interval -1 day),'%Y%m%d'),@group_row+1,1) as group_row, ";
        $sql .= "@sign_date:=FROM_UNIXTIME(add_time,'%Y%m%d') as sign_date,p.* from ";
        $sql .= "(select * from user_sign where user_id=".$userId." group by FROM_UNIXTIME(add_time,'%Y%m%d')) p, ";
        $sql .= "(select @row_no:=0,@group_row:=0,@sign_date:='') b ";
        $sql .= "order by add_time asc) a order by row_no desc limit 1";
        $result = M()->query($sql);
        $data = reset($result);
        $signDays = $data['group_row'];
        return $signDays ? $signDays : 0;
    }

}